package juan.happyge.plugin.myi.view.page;

import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.components.JBLabel;
import juan.happyge.plugin.myi.config.BookConfig;
import juan.happyge.plugin.myi.config.ConfigManage;
import juan.happyge.plugin.myi.pojo.book.ChapterPojo;
import juan.happyge.plugin.myi.service.book.BookCrawlerBuilder;
import juan.happyge.plugin.myi.service.book.BookService;
import juan.happyge.plugin.myi.utils.VerifyUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.concurrent.CompletableFuture;

public class BookView {

    BookConfig bookConfig = ConfigManage.getInstance().getBookConfig();

    private JPanel bookJPanel;
    private JLabel labChapter;
    private JTextPane txpBookContent;
    private JButton btnSelectChapter;
    private JButton btnPageUp;
    private JButton btnPageDown;
    private JButton btnJump;
    private JTextField txfLineNum;
    private JLabel labTotalLineNum;
    private JButton startSerialButton;

    public BookView(){

        btnJump.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(VerifyUtils.isNumber(txfLineNum.getText())){
                    jumpLine(Integer.parseInt(txfLineNum.getText()) - 1);
                }
            }
        });
        btnPageUp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jumpLine(bookConfig.getLineIndex() - bookConfig.getPageSize());
            }
        });
        btnPageDown.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jumpLine(bookConfig.getLineIndex() + bookConfig.getPageSize());
            }
        });
        txpBookContent.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                int keyCode = e.getKeyCode();
                if(37==keyCode){
                    jumpLine(bookConfig.getLineIndex() - bookConfig.getPageSize());
                }else if(39==keyCode){
                    jumpLine(bookConfig.getLineIndex() + bookConfig.getPageSize());
                }
            }
        });

        //开始连载
        startSerialButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CompletableFuture future = CompletableFuture.runAsync(() ->{
                    BookCrawlerBuilder bookCrawlerBuilder = new BookCrawlerBuilder();
                    bookCrawlerBuilder.startSerial();
                });
            }
        });

/*
        //另一种方式开发
        DefaultActionGroup group = (DefaultActionGroup) ActionManager.getInstance().getAction("ChapterActionGroup");
        ActionPopupMenu actionPopupMenu = ActionManager.getInstance().createActionPopupMenu(ActionPlaces.POPUP, group);
        JPopupMenu component = actionPopupMenu.getComponent();
        component.show(bookJPanel, 10, 10);*/


        btnSelectChapter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //点击创建一个面板
                JBPopupFactory instance = JBPopupFactory.getInstance();//创建JBPopupFactory实例
                JBPopup jbPopup = instance.createComponentPopupBuilder(new BookChapterView().panelBookChapter, new JBLabel())//参数说明：内容对象,优先获取
                        .setTitle("章节选择")
                        .setMovable(true)
                        .setResizable(true)
                        .setMinSize(new Dimension(300, 200))
                        .createPopup();
                jbPopup.showInCenterOf(bookJPanel);
            }
        });

        refreshBookViewUI();
    }

    public void refreshBookViewUI(){
        BookService.loadBook(bookConfig.getBookPath());
        labTotalLineNum.setText(BookService.getTotalLines().toString());
        jumpLine(bookConfig.getLineIndex());
    }

    public JPanel getBookJPanel() {
        return bookJPanel;
    }


    public void jumpLine(Integer lineIndex){
        if( lineIndex < 0 || lineIndex >= BookService.getTotalLines()){
            return;
        }
        String content = BookService.getBookParagraph(lineIndex, bookConfig.getPageSize());
        String chapterName = BookService.getChapterName(lineIndex, bookConfig.getPageSize());
        txpBookContent.setFont( new Font("",0, bookConfig.getFontSize()));
        txpBookContent.setText(content);
        txfLineNum.setText(String.valueOf(lineIndex  + 1));
        labChapter.setText(chapterName);
        bookConfig.setLineIndex(lineIndex);
    }

    public void jumpChapter(ChapterPojo chapterPojo){
        String content = BookService.getBookParagraph(chapterPojo.getLineIndex(), bookConfig.getPageSize());
        txpBookContent.setFont( new Font("",0, bookConfig.getFontSize()));
        txpBookContent.setText(content);
        txfLineNum.setText(String.valueOf(chapterPojo.getLineIndex() + 1));
        labChapter.setText(chapterPojo.getName());
        bookConfig.setLineIndex(chapterPojo.getLineIndex());
    }

}
